package com.jwthhealth_pub.old.sportfitness.view;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.common.FontManager;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.old.api.ApiHelper;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.common.ImageLoader;
import com.jwthhealth_pub.old.sportfitness.module.SportHomeModule;
import com.jwthhealth_pub.old.sportfitness.module.SprotCourseModule;
import com.jwthhealth_pub.old.sportfitness.view.adapter.SportCourseAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 健身课程列表
 * Created by Administrator on 2017/1/4.
 */
public class SportfitnessCourseListActivity extends Activity {
    @BindView(R.id.diffcult)
    TextView diffcult;
    @BindView(R.id.summary)
    TextView summary;
    @BindView(R.id.head_title)
    TextView title;
    @BindView(R.id.banner)
    RelativeLayout banner;
    @BindView(R.id.rb)
    RecyclerView rv;
    @BindView(R.id.progressbar)
    ProgressBar progressbar;
    @BindView(R.id.img)
    ImageView img;
    @BindView(R.id.sign_topbar)
    TitleLayout signTopbar;

    private SportHomeModule.DataBean dataBean;
    private boolean isRequest;
    private SprotCourseModule.DataBean headDataBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }

        setContentView(R.layout.activity_sportfinesslist);
        ButterKnife.bind(this);

        dataBean = (SportHomeModule.DataBean) getIntent().getExtras().getSerializable(Constant.SPORT_DATA);

        if (!isRequest) {
            isRequest = true;
            progressbar.setVisibility(View.VISIBLE);
            Call<SprotCourseModule> sprotCourseModuleCall = ApiHelper.sportCourseRequest(dataBean.getId());
            sprotCourseModuleCall.enqueue(new Callback<SprotCourseModule>() {
                @Override
                public void onResponse(Call<SprotCourseModule> call, Response<SprotCourseModule> response) {
                    SprotCourseModule body = response.body();
                    if (body.getCode().equals("0")) {
                        Message message = new Message();
                        Bundle bundle = new Bundle();
                        bundle.putSerializable(Constant.SPORT_DATA, body);
                        message.setData(bundle);
                        mHandler.sendMessage(message);
                    } else {
                        isRequest = false;
                        Log.d("SportfitnessCourseActiv", "fail");
                    }
                }

                @Override
                public void onFailure(Call<SprotCourseModule> call, Throwable t) {
                    isRequest = false;
                    Log.d("SportfitnessCourseActiv", "fail");
                }
            });
        }


        initTopbar();
    }

    private void initRv(List<SprotCourseModule.DataBean> data) {
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(new SportCourseAdapter(this, data));
    }

    private void initHead(SprotCourseModule.DataBean dataBean) {

//        Picasso.with(this).load(dataBean.getVideo_thumb()).into(img);
        ImageLoader.picasso(dataBean.getVideo_thumb(), img);
        title.setText(dataBean.getName());
        switch (dataBean.getLevel()) {
            case "1":
                diffcult.setText(getResources().getString(R.string.sport_diffcult) +
                        " " + getResources().getString(R.string.icon_sport_start) +
                        " " + getResources().getString(R.string.icon_sport_start_o) +
                        " " + getResources().getString(R.string.icon_sport_start_o) +
                        " " + getResources().getString(R.string.icon_sport_start_o));
                break;
            case "2":
                diffcult.setText(getResources().getString(R.string.sport_diffcult) +
                        " " + getResources().getString(R.string.icon_sport_start) +
                        " " + getResources().getString(R.string.icon_sport_start) +
                        " " + getResources().getString(R.string.icon_sport_start_o) +
                        " " + getResources().getString(R.string.icon_sport_start_o));
                break;
            case "3":
                diffcult.setText(getResources().getString(R.string.sport_diffcult) +
                        " " + getResources().getString(R.string.icon_sport_start) +
                        " " + getResources().getString(R.string.icon_sport_start) +
                        " " + getResources().getString(R.string.icon_sport_start) +
                        " " + getResources().getString(R.string.icon_sport_start_o));
                break;
            case "4":
                diffcult.setText(getResources().getString(R.string.sport_diffcult) +
                        " " + getResources().getString(R.string.icon_sport_start) +
                        " " + getResources().getString(R.string.icon_sport_start) +
                        " " + getResources().getString(R.string.icon_sport_start) +
                        " " + getResources().getString(R.string.icon_sport_start));
                break;
            default:
                break;
        }

        View[] views = new View[]{diffcult};
        FontManager.markAsIconContainer(views);

        summary.setText(dataBean.getSport_body() + " / " + dataBean.getHas_tools() + " / " + dataBean.getTime_long() + "分钟 / " + dataBean.getHits() + getResources().getString(R.string.sport_count));

    }

    private void initTopbar() {

        signTopbar.setTitle(getResources().getString(R.string.sport_title) + " " + dataBean.getName());

        signTopbar.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {
                finish();
            }

            @Override
            public void leftTextClick() {

            }

            @Override
            public void rightclick() {

            }

            @Override
            public void rightTextClick() {

            }
        });
    }

    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            SprotCourseModule body = (SprotCourseModule) msg.getData().getSerializable(Constant.SPORT_DATA);
            List<SprotCourseModule.DataBean> data = body.getData();
            headDataBean = data.get(0);
            initHead(headDataBean);
            data.remove(0);
            initRv(data);
            isRequest = false;
            progressbar.setVisibility(View.GONE);
        }
    };

    @OnClick(R.id.banner)
    public void onClick() {
        Intent intent = new Intent(SportfitnessCourseListActivity.this, SportCourseInfoActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(Constant.SPORT_DATA, headDataBean);
        intent.putExtras(bundle);
        startActivity(intent);
    }
}
